using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace TST.SharePointObjects
{
    public class TimerJobSettings : LayoutsPageBase
    {
        protected WebApplicationSelector selectWebApplication;
        protected Label labelStatus;
        protected TextBox editTitle;
        protected SchedulePicker schedulePicker;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!Page.IsPostBack)
            {
                this.OnSelectorContextChange(null, null);
            }
        }

        private void InitUI()
        {
            labelStatus.Text = String.Empty;
            schedulePicker.ScheduleString = String.Empty;
            editTitle.Text = String.Empty;
            schedulePicker.Enabled = true;
            editTitle.Enabled = true;
        }

        protected void buttonCancel_Click(object sender, EventArgs e)
        {
            SPUtility.Redirect("/_admin/applications.aspx", SPRedirectFlags.Default, this.Context);
        }

        protected void buttonOK_Click(object sender, EventArgs e)
        {
            if (selectWebApplication.CurrentItem != null)
            {
                SPJobDefinition timerjob = JobDefinition;
                if (timerjob == null)
                {
                    return;
                }
                timerjob.Title = editTitle.Text;
                timerjob.Schedule = (schedulePicker.IsValid) ? schedulePicker.Schedule : timerjob.Schedule;
                timerjob.Update();
                SPUtility.Redirect("/_admin/applications.aspx", SPRedirectFlags.Default, this.Context);
            }
        }

        private SPJobDefinition JobDefinition
        {
            get
            {
                SPWebApplication webApplication = selectWebApplication.CurrentItem;
                if (webApplication != null)
                {
                    foreach (SPJobDefinition job in webApplication.JobDefinitions)
                    {
                        if (job.Name == IndexerJobInstaller.INDEXERJOB_FEATURE_ID)
                        {
                            return job;
                        }
                    }
                }
                return null;
            }
        }

        protected void OnSelectorContextChange(object objSender, EventArgs evtargs)
        {
            InitUI();
            if (selectWebApplication.CurrentItem != null)
            {
                SPJobDefinition timerjob = JobDefinition;
                if (timerjob == null)
                {
                    labelStatus.Text = "The timer job is not available in this web application.";
                    schedulePicker.Enabled = false;
                    editTitle.Enabled = false;
                }
                else
                {
                    labelStatus.Text = String.Format("Timer job found: last run time={0}.", timerjob.LastRunTime == DateTime.MinValue ? "N/A" : timerjob.LastRunTime.ToString());
                    schedulePicker.ScheduleString = timerjob.Schedule.ToString();
                    editTitle.Text = timerjob.Title;
                }
            }
            else
            {
                labelStatus.Text = "No web application selected.";
                schedulePicker.Enabled = false;
                editTitle.Enabled = false;
            }
        }

    }
}
